/*******************************************************************************
 * Copyright (c) 2007 University of Oslo.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     University of Oslo - initial API and implementation
 *******************************************************************************/

package com.cea.papyrus.diagram.sequence.sedi.edit.command;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.GraphicalEditPart;
import org.eclipse.uml2.uml.InteractionFragment;
import org.eclipse.uml2.uml.Lifeline;

import com.cea.papyrus.core.utils.LookForElement;
import com.cea.papyrus.diagram.sequence.sedi.figures.InteractionFragmentFigure1;
import com.cea.papyrus.diagram.sequence.sedi.model.command.LifelinePosition;
import com.cea.papyrus.diagram.sequence.sedi.model.command.diagram.SetBoundsHintCommand;
import com.cea.papyrus.diagraminterchange2.di2.Diagram;
import com.cea.papyrus.diagraminterchange2.di2.GraphElement;
import com.cea.papyrus.diagraminterchange2.di2.GraphNode;

/**
 * TODO Document
 * 
 * @author frankd@ifi.uio.no
 * @deprecated Use analysis commands to determine position
 */
public class SetPreferredBoundsHintCommand extends SetBoundsHintCommand {

    private EditPart anEditPart;
    private InteractionFragment fragment;
    private List<LifelinePosition> covered;

    public void setEditPart(EditPart anEditPart) {
        this.anEditPart = anEditPart;
    }

    public void setCovered(List<LifelinePosition> covered) {
        this.covered = covered;
    }

    public void setFragment(InteractionFragment fragment) {
        this.fragment = fragment;
    }

    @SuppressWarnings("unchecked")
    public void execute() {
        if (!covered.isEmpty()) {
            Lifeline lifeline = ((LifelinePosition) covered.get(0)).getLifeline();

            // Find the fragment figure via the EditPart Registry
            // and ask it for the preferred bounds

            GraphElement viewElement = (GraphElement) anEditPart.getModel();
            Diagram diagram = LookForElement.getDiagram(viewElement);
            List<GraphNode> listLifeline =
                    LookForElement.lookForGraphElementWODiagram(lifeline, diagram, new ArrayList<GraphNode>(
                            1));
            GraphNode lifelineView = listLifeline.get(0);
            List<GraphNode> listFrag =
                    LookForElement.lookForGraphElementWODiagram(fragment, diagram, new ArrayList<GraphNode>(
                            1));
            GraphNode fragmentView = listFrag.get(0);

            GraphicalEditPart fragmentEP =
                    (GraphicalEditPart) anEditPart.getViewer().getEditPartRegistry().get(
                            fragmentView);
            InteractionFragmentFigure1 fragmentFigure =
                    (InteractionFragmentFigure1) fragmentEP.getFigure();

            Rectangle preferred =
                    fragmentFigure.getPreferredModelBounds(new Rectangle(
                            fragmentView.getPosition(), fragmentView.getSize()), new Rectangle(
                            lifelineView.getPosition(), lifelineView.getSize()));

            setHint(preferred);
            super.execute();
        }
    }
}
